package com.github.cdisk.xtrans.component;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.github.cdisk.xtrans.annotiation.Component;
import com.github.cdisk.xtrans.annotiation.ConnectLine;
import com.github.cdisk.xtrans.annotiation.DefaultLine;
import com.github.cdisk.xtrans.connect.Db;
import com.github.cdisk.xtrans.core.BaseComponent;
import com.github.cdisk.xtrans.core.ConfigFile;
import com.github.cdisk.xtrans.core.GlobalVariables;
import com.github.cdisk.xtrans.exception.BuildException;

/**
 * JSON文件导出
 * TODO 尚未实现
 * @author Cdisk
 *
 */
@Component(value="JSON输出（不可用）", icon="table.png", group="常用元件")
public class JsonExport extends BaseComponent {

	private static final Logger LOGGER = Logger.getLogger(JsonExport.class);

	private String file;

	/** 元件逐条执行过程 */
	@DefaultLine
	@ConnectLine(value="逐条处理")
	protected BaseComponent proccess;

	/** 元件执行错误后的执行过程_xml字符串id */
	private String _proccess;


	@Override
	public void build(ConfigFile config, GlobalVariables variables) throws BuildException {
		super.build(config, variables);
//		sql = variables.replaceVariables(sql); // 替换变量

		if (StringUtils.isEmpty(file)) {
			throw new BuildException(this.getClass().getName() + "(" +  this.id + ")未找到文件[file]说明。");
		}

		/* 检测逐条执行过程 */
		if (_proccess != null && _proccess.length() != 0) {
			proccess = config.findComponentById(_proccess);
			if (proccess == null) throw new BuildException(this.getClass().getName() + "(" +  this.id + ")未找到语句[proccess]说明：" + _proccess);
		}
	}

	@Override
	public int execute(Map<String, Object> input) {

		try {
			Db db = this.connect.getDb();
			db.open();

			ProccessResult rst = proccess(input); // 执行逐条处理
			if (this.success != null) {
				Map<String, Object> __map = new HashMap<String, Object>();
				__map.put("$parent", input);
				__map.put("$proccessCount", rst.count);
				__map.put("$successCount", rst.success);
				__map.put("$failedCount", rst.failed);
				__map.put("$errorCount", rst.error);
				this.success.execute(__map);
				__map.clear();
			}

			LOGGER.info("执行SQL:" + this.id + ", 执行成功.");
		} catch (SQLException e) {
			e.printStackTrace();
			return EXECUTE_ERROR;
		}

		return EXECUTE_SUCCESS;
	}

	/**
	 * 逐条处理过程
	 * @param struct
	 * @param input 
	 * @return
	 * @throws SQLException
	 */
	private ProccessResult proccess(Map<String, Object> input) throws SQLException {
		ProccessResult result = new ProccessResult();
		Map<String, Object> map;
		int count = 0;
		int success = 0;
		int failed = 0;
		int error = 0;

		result.count = count;
		result.success = success;
		result.failed = failed;
		result.error = error;

		return result;
	}

	@Override
	public boolean isConnectComponent(BaseComponent component) {
		if (super.isConnectComponent(component)) return true;
		if (proccess == component) return true;
		return false;
	}

	/**
	 * 设置逐条执行的元件
	 * @return
	 */
	public BaseComponent getProccess() {
		return proccess;
	}

	/**
	 * 设置逐条执行的元件
	 * @param proccess
	 */
	public void setProccess(BaseComponent proccess) {
		this.proccess = proccess;
	}

	/**
	 * 逐条执行结果
	 * @author Cdisk
	 *
	 */
	private static class ProccessResult {

		/** 执行总数 */
		public int count;

		/** 成功总数 */
		public int success;

		/** 失败总数 */
		public int failed;

		/** 错误总数 */
		public int error;

	}

}
